package com.beverages.commodities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beverages.commodities.common.Constants;
import com.beverages.commodities.common.Result;
import com.beverages.commodities.entity.Commodities;
import com.beverages.commodities.entity.dto.CommoditiesDTO;
import com.beverages.commodities.service.ICommoditiesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/merchant/commodities")
public class CommoditiesController {
    @Resource
    private ICommoditiesService commoditiesService;

    @GetMapping("/page")
    public Result<IPage<CommoditiesDTO>> findPage(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(defaultValue = "0") Integer type,
                                                  @RequestParam(defaultValue = "") String milkteaName,
                                                  @RequestParam(defaultValue = "true") Boolean whetherOnlyShowListedCommodities) {
        // 创建分页对象
        IPage<Commodities> page = new Page<>(pageNum, pageSize);

        // 创建查询条件
        QueryWrapper<Commodities> queryWrapper = new QueryWrapper<>();
        if(type != 0){
            queryWrapper.eq("type", type);
        }
        if (!"".equals(milkteaName)) {
            queryWrapper.like("milktea_name", milkteaName.trim());
        }
        if(whetherOnlyShowListedCommodities){
            queryWrapper.eq("whether_to_list",1);
        }

        // 执行分页查询
        IPage<Commodities> resultPage = commoditiesService.page(page, queryWrapper);

        // 将查询结果转换为 DTO 列表
        List<CommoditiesDTO> dtoList = resultPage.getRecords().stream().map(commodities -> {
            return commoditiesService.convertCommoditiesEntityToCommoditiesDTO(commodities);
        }).collect(Collectors.toList());

        // 创建新的 IPage 对象来封装 DTO 列表
        IPage<CommoditiesDTO> dtoPage = new Page<>(pageNum, pageSize, resultPage.getTotal());
        dtoPage.setRecords(dtoList);

        // 返回结果
        return Result.success(dtoPage);
    }

    //查看某一商品详情
//    @GetMapping("/{milkteaId}")
//    public Result findOne(@PathVariable Integer milkteaId) {
//        return Result.success(commoditiesService.getById(milkteaId));
//    }

    @GetMapping("/{milkteaId}")
    public Result<CommoditiesDTO> findOne(@PathVariable Integer milkteaId) {
        return Result.success(commoditiesService.convertCommoditiesEntityToCommoditiesDTO(commoditiesService.getById(milkteaId)));
    }

    @PostMapping    //格式在前端处理
    public Result save(@RequestBody Commodities commodities) {
        commoditiesService.saveOrUpdate(commodities);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delist(@PathVariable Integer id) {    //删除后，相关订单未完成的要接着制作饮料
        // 创建一个更新对象
        Commodities commodities = new Commodities();
        commodities.setWhetherToList(false); // 设置whether_to_list为false
        commodities.setId(id); // 设置要更新的记录的ID

        // 更新记录
        return Result.success(commoditiesService.updateById(commodities));
    }

//    @GetMapping("/relist/{id}")
//    public Result relist(@PathVariable Integer id) {
//        // 创建一个更新对象
//        Commodities commodities = new Commodities();
//        commodities.setWhetherToList(true); // 设置whether_to_list为true
//        commodities.setId(id); // 设置要更新的记录的ID
//
//        // 更新记录
//        return Result.success(commoditiesService.updateById(commodities));
//    }

    @PostMapping("/delist/batch")
    public Result delistBatch(@RequestBody List<Integer> ids) {
        // 创建更新条件
        LambdaUpdateWrapper<Commodities> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Commodities::getId, ids); // 设置要更新的ID列表
        updateWrapper.set(Commodities::getWhetherToList, false); // 将whether_to_list设置为false

        // 执行批量更新
        return Result.success(commoditiesService.update(updateWrapper));
    }

    @GetMapping("/id/{commodityId}")
    public Result getCommodityById(@PathVariable Integer commodityId) {
        return Result.success(commoditiesService.getById(commodityId));
    }
}
